package yxy.game.pm2.command.equip;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.BlacksmithMessage;
import yxy.game.pm2.module.BlacksmithHandle;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.PackageHandle;

import java.util.ArrayList;
import java.util.Collection;

/** 工坊 装备一键合成确认 */
@MessageType(proto = Command.EquipementFusionOneButtonCheck, classtype = BlacksmithMessage.EquipementFusionOneButtonCheckRequest.class)
public class CMD_EquipementFusionOneButtonCheck extends Command<BlacksmithMessage.EquipementFusionOneButtonCheckRequest> {

	@Override
	public void execute(MessageSource messageSource, BlacksmithMessage.EquipementFusionOneButtonCheckRequest request) {
		String playerCode = messageSource.getTag();
		int type = request.getType();

		BlacksmithMessage.EquipementFusionOneButtonCheckResponse.Builder response = BlacksmithMessage.EquipementFusionOneButtonCheckResponse.newBuilder();

		BlacksmithHandle blacksmithModule = new BlacksmithHandle();
		Currency cost = Currency.create(CurrencyType.CT_钱币, 0, 0);
		try {
			Collection<Currency> target = blacksmithModule.fusionAll(playerCode, type, cost);
			ArrayList<Currency> get = new ArrayList<>();
			for (Currency c : target) {
				if (c.getNumber() > 0) {
					get.add(c);
				}
			}

			try {
				new CurrencyHandle().cost(playerCode, CCReason.工坊, cost);
			} catch (CurrencyNotEnoughException e) {
				send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
				return;
			}

			ArrayList<Currency> add = new ArrayList<>();
			ArrayList<Currency> minus = new ArrayList<>();
			for (Currency t : target) {
				if (t.getNumber() > 0) {
					add.add(t);

				}
				if (t.getNumber() < 0) {
					t.setNumber(-t.getNumber());
					minus.add(t);
				}
			}
			new CurrencyHandle().addCurrency(playerCode, CCReason.工坊, add);
			new CurrencyHandle().cost(playerCode, CCReason.工坊, minus);

			response.addAllTarget(new CurrencyHandle().packItems(get));
			response.setPack(new PackageHandle().getPackage(playerCode, CurrencyType.CT_装备));

			send(messageSource, response, Result.Success);
		} catch (Exception e) {
			send(messageSource, response, Result.DataError);// 合成失败
		}

//		CurrencyType currencyType = CurrencyType.CT_装备;

//		PackageMessage.Item.Builder item = PackageMessage.Item.newBuilder();
//		item.setDataId(15515);
//		item.setNumber(2);
//		response.addTarget(item);
//
//		PackageMessage.Item.Builder item2 = PackageMessage.Item.newBuilder();
//		item2.setDataId(16116);
//		item2.setNumber(1);
//		response.addTarget(item2);
//
//		try {
//			PackageMessage.Package.Builder pack = new PackageHandle().getPackage(playerCode, currencyType);
//			response.setPack(pack);
//			send(messageSource, response, Result.Success);
//		} catch (Exception e) {
//			e.printStackTrace();
//			send(messageSource, response, Result.DataError);
//		}

	}

	private void send(MessageSource messageSource, BlacksmithMessage.EquipementFusionOneButtonCheckResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}